<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('任务列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="role-form">
                <div class="select-list">
                    <ul>
                        <li>
                            任务名称：<input type="text" name="taskName"/>
                        </li>
                        <li>
                            服务器Ip：<input type="text" name="serverIp"/>
                        </li>
                        <li>
                            应用名称：<input type="text" name="appName"/>
                        </li>
                        <li>
                            应用分组：<select name="longGroup" id = "longGroupSelect" >
                            <option value="">所有</option>
                        </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="openAdd()" >
                <i class="fa fa-plus"></i> 新增
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeGet()" >
                <i class="fa fa-remove"></i> 删除
            </a>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var prefix = ctx + "server";
    $(function() {
        $.when( appendGroup()).then(function() {
            console.log("所有方法按顺序执行完毕");
            setTimeout(function() {
                searchTable()// 延时执行
            }, 500);
        });

    });



    function  appendGroup() {
        //下拉框
        $.operate.get(ctx+"group/listGroup",function (res) {
            $.each(res.data, function(index, item) {
                $('#longGroupSelect').append('<option value="' + item.id + '">' + item.groupName + '</option>');
            });
        })
    }


    function openAdd() {
        // $.modal.openFullWithClose("发布流程",ctx+"page/pushTask/pushTaskAdd?updateFlag=0");
        var index = top.layer.open({
            type: 2,
            area: [ '800px',  '750px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: "任务信息",
            content: ctx+"page/pushTask/pushTaskAdd?updateFlag=0",
            btn: [ '关闭'],
            // 弹层外区域关闭
            shadeClose: true,
            closeBtn: false,
            yes:function(index, layero){
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.closePage();
                $.modal.close(index);
                setTimeout(function () {
                    $.table.search();
                }, 1000);
            },
            cancel: function(index) {
                return true;
            }
        });
        top.layer.full(index);
    }
    
    function  openEdit(id) {
        $.modal.openFullWithClose("发布流程",ctx+"page/pushTask/pushTaskEdit?id="+id+"&&updateFlag=1");
    }
    
    function openDetail(id) {
        $.modal.openFullWithClose("详情",ctx+"page/pushTask/pushTaskDetail?id="+id+"&&updateFlag=1");
    }
    
    function  openRunLog(id) {
        $.modal.openWithClose("执行日志",ctx+"page/pushTask/pushTaskLog?id="+id);
    }

    function restartTask(id) {
        $.modal.confirm("是否重启任务",function () {
            $.operate.ajaxJustGet(ctx+"task/restartTask?id="+id,function (res) {
                if(res.code == web_status.SUCCESS){
                    $.modal.msgSuccess("重启成功！");
                    $.table.refresh();
                }else {
                    $.modal.msgError(res.msg)
                }
            })
        })
    }

    function stopTask(id) {
        $.modal.confirm("是否终止任务",function () {
            $.operate.ajaxJustGet(ctx+"task/stopTask?id="+id,function (res) {
                if(res.code == web_status.SUCCESS){
                    $.modal.msgSuccess("终止任务成功！");
                    $.table.refresh();
                }else {
                    $.modal.msgError(res.msg)
                }
            })
        })
    }

    //查询表格
    function  searchTable() {
        var options = {
            url: ctx + "task/pageList",
            createUrl: ctx+ "page/pushTask/pushTaskAdd",
            updateUrl:  ctx+"page/confs/groupEdit",
            removeUrl:   ctx+"task/removeOldTask",
            modalName: "任务列表",
            columns: [{
                checkbox: true

            },
                {
                    field: 'id',
                    visible: false
                },
                {
                    title: '任务环节',
                    formatter: function(value, row, index) {
                        if(row.pushStep == -1){
                            return '<button type="button" class="btn btn-w-m btn-danger">'+row.pushStepName+'</button>'
                        }else {
                            return '<button type="button" class="btn btn-w-m btn-success">'+row.pushStepName+'</button>'
                        }
                    }
                },
                {
                    field: 'taskName',
                    title: '任务名称'
                },
                {
                    field: 'taskDes',
                    title: '任务描述'
                },
                {
                    field: 'basicPath',
                    title: '暂存路径'
                },
                {
                    field: 'pushTime',
                    title: '发布时间'
                },
                {
                    field: 'pushResName',
                    title: '发布结果'
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs " href="javascript:void(0)" onclick="openEdit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        var more = [];
                        if(row.optCmd == 0){
                            more.push("<a class='btn btn-success btn-xs' href='javascript:void(0)' onclick='restartTask(" + row.id + ")'><i class='fa fa-hourglass-start'></i>启动任务</a> ");
                        }else if(row.optCmd == 1){
                            more.push("<a class='btn btn-danger btn-xs' href='javascript:void(0)' onclick='stopTask(" + row.id + ")'><i class='fa fa-close'></i>终止任务</a> ");
                        }

                        more.push("<a class='btn btn-primary btn-xs' href='javascript:void(0)' onclick='openDetail(" + row.id + ")'><i class='fa fa-bars'></i>查看详情</a> ");
                        more.push("<a class='btn btn-primary btn-xs' href='javascript:void(0)' onclick='openRunLog(" + row.id + ")'><i class='fa fa-history'></i>执行日志</a> ");
                        actions.push('<a tabindex="0" class="btn btn-info btn-xs" role="button" data-container="body" data-placement="left" data-toggle="popover" data-html="true" data-trigger="hover" data-content="' + more.join('') + '"><i class="fa fa-chevron-circle-right"></i>更多</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    }
</script>
</body>
</html>